package com.easysoft.framework.copyright.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.copyright.mapper.CopyrightMapperExtd;
import com.easysoft.framework.copyright.model.CopyrightQuery;
import com.easysoft.framework.copyright.model.CopyrightView;
import com.easysoft.framework.copyright.model.SysCopyright;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("service.CopyrightService")
public class CopyrightService {
    @Autowired
    @Qualifier("mapper.CopyrightMapperExtd")
    private CopyrightMapperExtd copyrightMapperExtd;

    @DataSource(DataSource.SLAVE)
    public List<CopyrightView> selectCopyrightByPage(Page page, CopyrightQuery query) {
        int maxRow = copyrightMapperExtd.selectCountCopyright(query);
        if (maxRow == 0){
            return new ArrayList<CopyrightView>();
        }
        page.setMaxRow(maxRow);
        return copyrightMapperExtd.selectCopyrightByPage(page,query);
    }

    @DataSource(DataSource.SLAVE)
    public SysCopyright selectByPrimaryKey(String id) {
        return copyrightMapperExtd.selectByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int deleteSysCopyright(String id) {
        return copyrightMapperExtd.deleteByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int addSysCopyright(SysCopyright copyright) {
        copyright.setId(UUIDUtils.getUUID());
        copyright.setCreateTime(new Date());
        return copyrightMapperExtd.insertSelective(copyright);
    }

    @DataSource(DataSource.MASTER)
    public int updateSysCopyright(SysCopyright copyright) {
        return copyrightMapperExtd.updateByPrimaryKeySelective(copyright);
    }
}
